package com.example.categoryservice.controller;

import com.example.categoryservice.service.CategoryService;
import com.example.pojo.dto.CategoryInsertDTO;
import com.example.pojo.dto.CategoryPageQueryDTO;
import com.example.pojo.entity.Category;
import com.example.pojo.entity.result.PageResult;
import com.example.pojo.entity.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/admin/category")
@Api("图书分类相关接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询图书分类列表
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("分页查询图书分类列表")
    public Result<PageResult<Category>> list(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询图书分类列表");
        PageResult<Category> result = categoryService.list(categoryPageQueryDTO);
        log.info("查询成功：{}", result);
        return Result.success(result);
    }

    /**
     * 新增图书分类
     *
     * @return
     */
    @PostMapping
    @ApiOperation("新增图书分类")
    public Result<Long> insertCategory(@RequestBody CategoryInsertDTO categoryInsertDTO) {
        log.info("新增图书分类：{}", categoryInsertDTO);
        Long categoryId = categoryService.insertCategory(categoryInsertDTO);
        log.info("操作完成");
        return Result.success(categoryId);
    }

    /**
     * 减少库存
     *
     * @param id
     * @return
     */
    @PutMapping("/decrease/{id}")
    @ApiOperation("减少库存")
    public Result decrease(@PathVariable Long id) {
        log.info("减少库存：{}", id);
        categoryService.decrease(id);
        log.info("操作完成");
        return Result.success();
    }

    /**
     * 增加库存
     *
     * @param id
     */
    @PutMapping("/plus/{id}")
    public Result plus(@PathVariable Long id) {
        log.info("增加库存：{}", id);
        categoryService.plus(id);
        log.info("操作完成");
        return Result.success();
    }
}
